package io.geekidea.springbootplus.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.geekidea.springbootplus.system.entity.Customer;
import io.geekidea.springbootplus.system.mapper.CustomerMapper;
import io.geekidea.springbootplus.system.service.CustomerService;
import io.geekidea.springbootplus.system.param.CustomerPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *  服务实现类
 *
 * @author miko
 * @since 2020-10-10
 */
@Slf4j
@Service
public class CustomerServiceImpl extends BaseServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveCustomer(Customer customer) throws Exception {
        return super.save(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateCustomer(Customer customer) throws Exception {
        return super.updateById(customer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteCustomer(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public Paging<Customer> getCustomerPageList(CustomerPageParam customerPageParam) throws Exception {
        Page<Customer> page = new PageInfo<>(customerPageParam, OrderItem.desc(getLambdaColumn(Customer::getCreateTime)));
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        IPage<Customer> iPage = customerMapper.selectPage(page, wrapper);
        return new Paging<Customer>(iPage);
    }
    public Customer getByOpenID(String  openid){
        QueryWrapper<Customer> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        Customer customer = this.getOne(wrapper);
        return customer;
    }
}
